package com.hjw.dynamicdatasource.config;

import lombok.Data;
import lombok.Getter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ivan(黄俊玮)
 * FileName: DynamicCacheManager
 * Date: 2025/10/31 15:48
 */
@Getter
@Component
public class DynamicCacheManager {
    /**
     * 缓存已创建的Mapper实例
     */
    private Map<Class<?>, Object> mapperCache = new HashMap<>();

    /**
     * 缓存已注册的表信息
     */
    private Map<Class<?>, String> tableNameCache = new HashMap<>();

    public Object getMapperCache(Class<?> clazz){
        return mapperCache.get(clazz);
    }

    public void putMapperCache(Class<?> clazz, Object mapper){
        mapperCache.put(clazz, mapper);
    }

    public String getTableNameCache(Class<?> clazz){
        return tableNameCache.get(clazz);
    }

    public void putTableNameCache(Class<?> clazz, String tableName){
        tableNameCache.put(clazz, tableName);
    }

    public void clear(){
        mapperCache.clear();
        mapperCache=new HashMap<>(1);
        tableNameCache.clear();
        tableNameCache=new HashMap<>(1);
    }


}
